package com.example.cookie02.dao.imp;

import com.example.cookie02.dao.PersonDao;
import com.example.utrl.BaseDao;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ：lqs
 * @date ：Created in 2023/8/30 11:55
 * @description：
 * @modified By：
 * @version:
 */
public class PersonDaoImpl implements PersonDao {
    BaseDao baseDao = new BaseDao();

//查询所有
    @Override
    public List<Map<String, Object>> queryAll() {
        String sql = "select * from person";
        return baseDao.executeQuery(sql);
    }
//查询单个
    @Override
    public List<Map<String, Object>> queryOne(String id) {
        String sql = "select * from person where id=?";
        return baseDao.executeQuery(sql,id);

    }

    @Override
    public List<Map<String, Object>> queryName(String username) {
        String sql = "select * from person where username=?";
        return baseDao.executeQuery(sql,username);
    }

    //删除根据ID
    @Override
    public Boolean delect(int id) {
        String sql = "delete from person  where id=?;";
        return baseDao.executeUpdate(sql,id);
    }
//修改
    @Override
    public Boolean update(Map<String, String[]> parameterMap) {
        String sql = "UPDATE person SET username = ?,password = ?,createtime=?,phone=? WHERE id = ?;";
        return  baseDao.executeUpdate(sql,parameterMap.get("username")[0],parameterMap.get("password")[0],parameterMap.get("createtime")[0],parameterMap.get("phone")[0],parameterMap.get("id")[0]);
    }
//插入
    @Override
    public Boolean add(Map<String,String[]> objectMap) {
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formatNow = sdf.format(now);
        String sql = "insert into  person (username, password, createtime, phone) VALUES (?,?,?,?);";
        return baseDao.executeUpdate(sql,objectMap.get("username")[0],objectMap.get("password")[0],formatNow,objectMap.get("phone")[0]);
    }

    @Override
    public int AllCount() {
        List<Map<String, Object>> list = this.queryAll();
        return list.size();
    }

    @Override
    public List<Map<String, Object>> queryOnePage(int pageIndex,int pageSize) {
        String sql = " select * from person limit ?,?;";
        return baseDao.executeQuery(sql,(pageIndex-1)*pageSize,pageSize);

    }

    public static void main(String[] args) {


    }


}

